<?php
$this->load->view('admin/layout/head', array('title'=>"订单管理",'nav'=>'order'), FALSE);
$this->load->helper('form');
?>
<style>
.list-group-item .good-img {
	width: 100px;
	height: 100px;
	float: left;
	margin-right: 5px;
}
</style>
<div id="container">
	<!-- <h4>订单详情</h4>
	<a onclick="window.history.back()" class="btn btn-primary">返回</a> -->
	<div class="row">
		<div class="col-md-4 col-lg-4">
			<div class="panel panel-default	">
				<div class="panel-heading">
					<b>订单信息</b>
					<a href="javascript:window.history.back()" class="pull-right">返回</a>
				</div>
				<ul class="list-group">
					<li class="list-group-item clearfix">
						<div class="pull-left">订单编号：</div>
						<div class="pull-right"><?= $order['order_id'] ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">下单时间：</div>
						<div class="pull-right"><?= $order['create_time'] ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">订单总额：</div>
						<div class="pull-right"><?= $order['total'] ?></div>
					</li>
					<?php if($order['cut_fee']>0): ?>
					<li class="list-group-item clearfix">
						<div class="pull-left">优惠金额：</div>
						<div class="pull-right"><?= $order['cut_fee'] ?>(<?= $order['discount']*10 ?>折)</div>
					</li>
					<?php endif; ?>
					<li class="list-group-item clearfix">
						<div class="pull-left">送货费用：</div>
						<div class="pull-right"><?= $order['shipping_fee'] ?></div>
					</li>

					<li class="list-group-item clearfix">
						<div class="pull-left">余额支付：</div>
						<div class="pull-right"><?= $order['money'] ?></div>
					</li>
					<?php if($order['pay_fee']>0): ?>
					<li class="list-group-item clearfix">
						<div class="pull-left">其他支付：</div>
						<div class="pull-right">
							<?= $order['pay_fee'] ?>
							(<?php echo $pay_types[$order['pay_mode']]['chinese'] ?>)
						</div>
					</li>
					<?php endif; ?>
					<?php if($order['refund_fee']>0): ?>
					<li class="list-group-item clearfix">
						<div class="pull-left">退款金额：</div>
						<div class="pull-right">
							<?= $order['refund_fee'] ?>
						</div>
					</li>
					<?php endif; ?>
					<li class="list-group-item clearfix">
						<div class="pull-left">商品总量：</div>
						<div class="pull-right"><?= $order['weight'] ?>克</div>
					</li>

					<li class="list-group-item clearfix">
						<div class="pull-left">订单状态：</div>
						<div class="pull-right"><?= get_order_status($order['status']) ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">订单备注：</div>
						<div class="pull-right"><?= $order['remark'] ?></div>
					</li>
					<?php if ($cashier): ?>
						<li class="list-group-item clearfix">
							<div class="pull-left">收银员：</div>
							<div class="pull-right"><?= $cashier['username'] ?></div>
						</li>
					<?php endif ?>
				</ul>
			</div>

			<?php if ($user): ?>
			<div class="panel panel-default	">
				<div class="panel-heading">
					<b>用户信息</b>
				</div>
				<ul class="list-group">
					<li class="list-group-item clearfix">
						<div class="pull-left">姓名：</div>
						<div class="pull-right"><?= $user['username'] ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">手机：</div>
						<div class="pull-right"><?= $user['phone'] ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">邮箱：</div>
						<div class="pull-right"><?= $user['email'] ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">余额：</div>
						<div class="pull-right"><?= $user['balance'] ?></div>
					</li>
				</ul>
			</div>
			<?php endif ?>

			<?php if ($address): ?>
			<div class="panel panel-default	">
				<div class="panel-heading">
					<b>收货信息</b>
				</div>
				<ul class="list-group">
					<li class="list-group-item clearfix">
						<div class="pull-left">收货人姓名：</div>
						<div class="pull-right"><?= $address['accept_name'] ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">收货人手机：</div>
						<div class="pull-right"><?= $address['mobile'] ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">收货区域：</div>
						<div class="pull-right"><?= $address->province_region()['name'] ?>
							<?= $address->city_region()['name'] ?>
							<?= $address->area_region()['name'] ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">详细地址：</div>
						<div class="pull-right"><?= $address['address'] ?></div>
					</li>
					<li class="list-group-item clearfix">
						<div class="pull-left">收货时间：</div>
						<div class="pull-right"><?= $order['shipping_date'].' '.$order['shipping_time'] ?></div>
					</li>
				</ul>
			</div>
			<?php endif ?>

		</div>
		<div class="col-md-8 col-lg-8">
			<div class="panel panel-default	">
				<div class="panel-heading">
					<b>商品信息</b>
				</div>
				<form action="#" id="order-items-form">
				<input type="hidden" name="id" value="<?php echo $order['id'] ?>">
				<table class="table">
					<thead>
						<tr>
							<th>商品</th>
							<th>预定数量</th>
							<th>单价</th>
							<th>退货数</th>
						</tr>
					</thead>
					<tbody>
						<?php
						$total = 0;
						$qty = 0;
						foreach ($items as $key => $item):
						$total += $item['price'] * ($item['qty'] - $item['refund_qty']);
						$qty += $item['qty'] - $item['refund_qty'];
						?>
						<tr class="order-item order-item-<?php echo $item['id']?>">
							<td>
								<input type="hidden" name="item[id][]" value="<?= $item['id'] ?>">
								<span class="order-detail-item"><?= $item['product']['title'] ?></span>
								<span class="order-detail-item" style="color: #f30">[<?= $item['item']['title'] ?>]</span>
							</td>
							<td>
								<?php if($order['status']<2): ?>
								<input type="number" class="qty" name="item[qty][]" value="<?= $item['qty']*1 ?>" style="width:60px"/>
								<?php else: ?>
								<?php echo $item['qty']*1 ?>
								<?php endif; ?>
							</td>
							<td>
								<?php if($order['status']<2): ?>
								<input type="number" class="price" name="item[price][]" value="<?= $item['price']*1 ?>" style="width:60px"/>
								<?php else: ?>
								<?php echo $item['price']*1 ?>
								<?php endif; ?>
							</td>
							<td>
								<input type="hidden" class="refund_qty" value="<?= $item['refund_qty']*1 ?>"/>
								<?= $item['refund_qty']*1 ?>
							</td>
						</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
				</form>

				<div class="panel-footer products-footer" style="height: 50px">
					<span>
						合计：<em class="product-total"><?php echo $total?></em>
							<em> 总数量: <?= $qty ?></em>
					</span>
					<?php if($order['status'] < 2): ?>
					<a href="javascript:;" class="btn btn-primary pull-right change-product-btn" style="margin-left: 10px;">确定更改</a>
					<?php endif; ?>
					<?php if($order['status'] == 1): ?>
					<a href="javascript:;" class="btn btn-danger pull-right shipping-btn" style="margin-left: 10px;">设置为已发货</a>
					<?php endif; ?>
				</div>
			</div>
		</div>

		<div class="col-md-8 col-lg-8">
			<div class="panel panel-default	">
				<div class="panel-heading">
					<b>退换货信息</b>
				</div>
				<form action="#" id="order-items-form">
				<input type="hidden" name="id" value="<?php echo $order['id'] ?>">
				<table class="table">
					<thead>
						<tr>
							<th>退货单号</th>
							<th>订单号</th>
							<th>日期</th>
							<th>会员</th>
							<th>退款方式</th>
							<th>退款金额</th>
							<th>上次余额</th>
							<th>当前余额</th>
							<th>vat</th>
							<th>是否货损</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ($order_refunds as $order_refund): ?>
						<tr class="order-ref order-ref-<?php echo $order_refund['id']?>">
							<td><?= $order_refund['id'] ?></td>
							<td><?= $order_refund['order']['order_id'] ?></td>
							<td><?= $order_refund['create_time'] ?></td>
							<td><?= $order_refund['user'] ? $order_refund['user']['last_name'].$order_refund['user']['first_name'] : '' ?></td>
							<td><?= $pay_types[$order_refund['pay_mode']][$this->session->admin_language] ?></td>
							<td><?= $order_refund['money'] ?></td>
							<td><?= $order_refund['last'] ?></td>
							<td><?= $order_refund['now'] ?></td>
							<td><?= $order_refund['vat'] ?></td>
							<td><?= $order_refund['is_damaged'] ? '是' : '否' ?></td>
							<td>
								<a href="<?= site_url('admin/refund/detail/'.$order_refund['id']); ?>">查看详情</a>
							</td>
						</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
				</form>

			</div>
		</div>

	</div>

</div>
<?php
$this->load->view('admin/layout/footer');
?>
<script>
var id = <?= $order['id'] ?>;
var order_status = <?php echo $order['status'] ?>;
var old_product_total = <?php echo $total?$total:0; ?>;
var product_total = <?php echo $total?$total:0; ?>;
var order_discount = <?php echo $order['discount']>0?$order['discount']:1 ?>;
var user_balance = <?php echo $user['balance']?$user['balance']:0 ?>;
$(function () {
	$('.list-group-item .save').on('click', function () {
		var parent = $(this).parents('.list-group-item');
		var id = parent.attr('data-id');
		var price = parent.find('.price').val();
		var qty = parent.find('.qty').val();
		$.ajax({
			url: '/admin/orderItems/save/'+id,
			type: 'post',
			data: {
				price: price,
				qty: qty
			},
			success: function (data) {
				alert('修改成功');
				// window.location.reload();
			},
			error: function (xhr) {
				alert(JSON.parse(xhr.responseText).error_msg);
			}
		})
	});

	$('input.qty,input.price').on('blur',function(){
		var val = parseFloat($(this).val()).toFixed(2);
		$(this).val(val*1);
		getProductsTotal();
	})

	$('.change-product-btn').on('click',function(){
		var confirm_msg = '';
		if(order_status<1 || old_product_total == product_total){
			confirm_msg = '确定要修改价格和数量吗？';
		}

		if(old_product_total > product_total && order_status == 1){
			var refund_money = (old_product_total - product_total) * order_discount;
			refund_money = refund_money.toFixed(2);
			confirm_msg = '修改后需要退款给用户￡'+refund_money+',确定要修改吗？';
		}

		if(old_product_total < product_total && order_status == 1){
			var more_money = product_total - old_product_total;
			if(more_money<=user_balance){
				confirm_msg = '修改后需扣除用户余额￡'+more_money+',确定要修改吗？';
			}else{
				confirm_msg = '修改后需扣除用户余额￡'+more_money+',用户余额不足，是否仍要修改？';
			}
		}

		confirm(confirm_msg,function(e){
			if(e.index == 1){
				$.post('/admin/order/change_items', $('#order-items-form').serialize(), function(data, textStatus, xhr) {
					Toast.success('修改成功',function(){
						window.location.reload();
					})
				});
			}
		})
	})

	$('.shipping-btn').on('click',function(){
		confirm('确定要设置为已发货吗？',function(e){
			if(e.index == 1){
				$.post('/admin/order/send/<?php echo $order['id'] ?>', function(data, textStatus, xhr) {
					Toast.success('设置发货成功',function(){
						window.location.reload();
					})
				});
			}
		})
	})
});

function getProductsTotal(){
	product_total = 0;
	$('.order-item').each(function(index, el) {
		var qty = parseFloat($(this).find('.qty').val());
		var price = parseFloat($(this).find('.price').val());
		var refund_qty = parseFloat($(this).find('.refund_qty').val());

		if(qty<refund_qty){
			$(this).find('.qty').val(refund_qty);
			qty = refund_qty;
			Toast.error('商品数量不能小于退货数量');
		}

		product_total += (qty - refund_qty) * price;
	});
	product_total = product_total.toFixed(2);
	$('.product-total').text(product_total);
}
</script>
